package com.example.zhaojianxu.signin.signIn.sign.view

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.zhaojianxu.signin.R
import com.example.zhaojianxu.signin.common.bean.BaseFragment
import com.example.zhaojianxu.signin.signIn.sign.adapter.SignAdapter
import com.example.zhaojianxu.signin.signIn.sign.model.SignEntity
import com.example.zhaojianxu.signin.signIn.sign.viewModel.SignViewModel
import kotlinx.android.synthetic.main.fragment_sign.*

class SignFragment : BaseFragment(), SwipeRefreshLayout.OnRefreshListener {

    var mAdapter: SignAdapter? = null
    var mList: MutableList<SignEntity> = mutableListOf()
    private lateinit var mViewModel: SignViewModel
    private lateinit var mView: View

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mView = layoutInflater.inflate(R.layout.fragment_sign, container, false)
        return mView
    }

    override fun initData() {
        mViewModel = ViewModelProviders.of(this)[SignViewModel::class.java]
        mAdapter = SignAdapter(mList)

        sign_recycler.adapter = mAdapter

        mViewModel.mSign.observe(this, Observer {
            mList.clear()
            it?.forEach {
                mList.add(it)
            }
            sign_swipe_refresh.isRefreshing = false
            mAdapter?.notifyDataSetChanged()
        })
    }

    override fun initView() {
        sign_recycler.layoutManager = LinearLayoutManager(this.context)

        sign_swipe_refresh.setColorSchemeResources(R.color.colorAccent,R.color.colorPrimary,R.color.colorActionBar)
        sign_swipe_refresh.setOnRefreshListener(this)
    }

    override fun onRefresh() {
        mViewModel.refresh()
    }

    override fun onResume() {
        super.onResume()
        mViewModel.refresh()
    }
}
